package cn.dnui_xqh414.dnuifood_16110100414.Fragment;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import cn.dnui_xqh414.dnuifood_16110100414.R;
import cn.dnui_xqh414.dnuifood_16110100414.bean.AllshopBean;
import cn.dnui_xqh414.dnuifood_16110100414.fragment_adapter_and_vhoder.rv_Adapter;
import cn.dnui_xqh414.dnuifood_16110100414.listener.OnRetrofitListener;
import cn.dnui_xqh414.dnuifood_16110100414.model.ShopModel;

/**
 * A simple {@link androidx.fragment.app.Fragment} subclass.
 */
public class ShopFragment
        extends androidx.fragment.app.Fragment
        implements OnRetrofitListener<List<AllshopBean>> {

    private RecyclerView recyclerView;
    private List<AllshopBean> allshopBeanList;
    private rv_Adapter rvAdapter;

    //获取布局文件
//    private LayoutInflater inflater;
//    private View layout;

    public ShopFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_shop,
                container, false);

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initRecyclerView();
        initView();

        ShopModel shopModel = new ShopModel();
        shopModel.getAllshops(this);

    }

    private void initRecyclerView() {
        recyclerView =
                getView().findViewById(R.id.rv_shop);


        RecyclerView.LayoutManager layoutManager =
                new LinearLayoutManager(getContext());

        recyclerView.setLayoutManager(layoutManager);

        recyclerView.setHasFixedSize(true);

        rvAdapter = new rv_Adapter(getContext());

//        allshopBeanList = getData();
//
//        rvAdapter.setList(allshopBeanList);

        recyclerView.setAdapter(rvAdapter);
    }

//    private List<AllshopBean> getData() {
//        List<AllshopBean>list = new ArrayList<AllshopBean>();
//        AllshopBean allshopBean;
//        allshopBean = new AllshopBean();
//        for(int i=0; i<5 ; ++i){
//            allshopBean = new AllshopBean();
//            allshopBean.setShopname("title"+i);
//            list.add(allshopBean);
//        }
//        return list;
//    }

    private void initView(){
//        aFloat = ratingBar.getRating();
//        Log.e("ShopFragment", String.valueOf(aFloat));
//        inflater = (LayoutInflater) getContext().
//                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//        layout = inflater.inflate(R.layout.rv_item, null);
//
//        ratingBar = layout.findViewById(R.id.rt_bar);
//        textView = layout.findViewById(R.id.tv_shop_title);
//        textView.setText("xiaolaodi");
//        ratingBar.setRating(3);
    }

    @Override
    public void onSuccess(List<AllshopBean> object, int flag) {
        if (flag == 3)
        allshopBeanList = object;
        rvAdapter.setList(allshopBeanList);  //可以直接set object 此处方便全局能用，先赋给了allshopBeanlist /*@samuiyuki 16110100414*/

    }

    @Override
    public void onFailure(String msg) {
        Toast.makeText(getContext(), "网络错误, 请联网刷新重试 !", Toast.LENGTH_SHORT).show();
    }
}
